<template>
    <div class="app-container">
       <el-form :model="queryParams" ref="queryRef" v-show="showSearch" :inline="true" label-width="120px">
          <el-form-item label="用户ID/手机号：" prop="keyword">
             <el-input
                v-model="queryParams.keyword"
                placeholder="请输入"
                clearable
                style="width: 240px"
                @keyup.enter="handleQuery"
             />
          </el-form-item>
          <el-form-item>
             <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
             <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
       </el-form>
       <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
             <el-button
                type="primary"
                plain
                icon="Plus"
                @click="handleAdd"
                v-hasPermi="['system:role:add']"
             >创建新用户</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
       </el-row>
 
       <!-- 表格数据 -->
       <el-table v-loading="loading" :data="roleList">
          <el-table-column label="" prop="roleId" width="100">
              <template #default="scope">
                <span class="idx"  :style="{ backgroundColor: scope.$index < 3 ? 'black' : 'gray' }">
                  {{scope.$index + 1}}
                </span>
              </template>
          </el-table-column>
          <el-table-column label="用户头像" prop="faceURL" width="100">
            <template #default="scope">
              <span class="avatar" v-if="scope.row.faceURL === ''">
                  {{scope.row.nickname[0]}}
              </span>
              <div v-else>
                <img :src="scope.row.faceURL" alt="light" />
              </div>
            </template>
          </el-table-column>
          <el-table-column label="用户呢称" prop="nickname" :show-overflow-tooltip="true" />
          <el-table-column label="用户ID" prop="userID" />
          <el-table-column label="性别" prop="gender" >
            <template #default ="scope">
                  <span>{{ gender(scope, scope.row.gender)  }}</span>
            </template>
          </el-table-column>
          <el-table-column label="手机号" prop="phoneNumber" />
          <el-table-column label="在线状态" prop="account">
             <template #default="scope">
                  {{scope.row.account === "" ? '-' : scope.row.account}}
             </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
             <template #default="scope">
                 <el-button link type="primary"  @click="handleUpdate(scope.row)">编辑</el-button>
                 <el-button link type="primary"  @click="handleDataScope(scope.row)">重置密码</el-button>
                 <el-button link type="primary"  @click="handleDelete(scope.row)">禁用</el-button>
             </template>
          </el-table-column>
       </el-table>
 
       <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="pageObj.pageNumber"
          v-model:limit="pageObj.showNumber"
          @pagination="getList"
       />
 

       <el-drawer :title="title" v-model="open" direction="rtl" destroy-on-close>
          <div class="upload_c">
            <div class="upload">
                <el-icon :size="20"><plus /></el-icon>
            </div>
            <div class="upload_text">点击上传</div>
          </div>
          
          <el-form ref="roleRef" :model="form" :rules="rules" label-width="100px">
             <el-form-item label="用户昵称" prop="nickname">
                <el-input v-model="form.nickname" placeholder="请输入角色名称" />
             </el-form-item>
             <el-form-item label="性别" prop="gender">
                <el-select v-model="form.gender" placeholder="请选择">
                    <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                    >
                    </el-option>
                </el-select>
             </el-form-item>
             <el-form-item label="生日" prop="brith">
                <el-date-picker v-model="form.brith" type="date" placeholder="选择日期" />
             </el-form-item>
             <el-form-item label="手机号" v-show="type == 'add'" prop="phoneNumber">
                <el-input placeholder="请输入手机号" v-model="form.phoneNumber" class="input-with-select">
                    <template #prepend>
                       +86
                    </template>
                </el-input>
             </el-form-item>
             <el-form-item label="密码" prop="password" v-show="type == 'add'">
                <el-input show-password v-model="form.password" placeholder="请输入密码" />
             </el-form-item>
          </el-form>
          <template #footer>
             <div class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
             </div>
          </template>
       </el-drawer>
 
       <el-dialog title="密码" v-model="openDataScope" width="500px" append-to-body>
          <el-form :model="form" label-width="0px">
             <el-form-item label="">
                <el-input show-password v-model="form.roleName" placeholder="请输入密码" />
             </el-form-item>
          </el-form>
          <template #footer>
             <div class="dialog-footer">
                <el-button type="primary" @click="submitDataScope">确 定</el-button>
                <el-button @click="cancelDataScope">取 消</el-button>
             </div>
          </template>
       </el-dialog>
    </div>
 </template>
 
 <script setup name="user">
 import { getUserList } from "@/api/chat/user";
 import { roleMenuTreeselect, treeselect as menuTreeselect } from "@/api/system/menu";
import { ref } from "vue";
 
 const router = useRouter();
 const { proxy } = getCurrentInstance();
 
 const roleList = ref([]);
 const open = ref(false);
 const loading = ref(true);
 const showSearch = ref(true);
 const ids = ref([]);
 const single = ref(true);
 const multiple = ref(true);
 const total = ref(0);
 const title = ref("");
 const type = ref('add')
 const dateRange = ref([]);
 const menuOptions = ref([]);
 const menuExpand = ref(false);
 const menuNodeAll = ref(false);
 const deptExpand = ref(true);
 const deptNodeAll = ref(false);
 const deptOptions = ref([]);
 const openDataScope = ref(false);
 const menuRef = ref(null);
 const deptRef = ref(null);
 const select = ref("1");
 const options = [
    {
        value: 1,
        label: '男',
    },
    {
        value: 2,
        label: '女',
    },
 ]
 
 /** 数据范围选项*/
 const dataScopeOptions = ref([
   { value: "1", label: "全部数据权限" },
   { value: "2", label: "自定数据权限" },
   { value: "3", label: "本部门数据权限" },
   { value: "4", label: "本部门及以下数据权限" },
   { value: "5", label: "仅本人数据权限" }
 ]);
 
 const data = reactive({
   form: {},
   queryParams: {
    keyword:undefined
   },
   pageObj:{
      pageNumber: 1,
      showNumber: 10,
   },
   rules: {
     roleName: [{ required: true, message: "用户昵称不能为空", trigger: "blur" }],
     iphone: [{ required: true, message: "手机号不能为空", trigger: "blur" }],
     password: [{ required: true, message: "密码不能为空", trigger: "blur" }]
   },
 });
 const { queryParams, form, rules, pageObj } = toRefs(data);
 
 /** 查询用户列表 */
 function getList() {
   loading.value = true;
   const parmas = {
    pagination: data.pageObj,
    normal: 1,
    keyword: data.queryParams.keyword
   }
   getUserList(parmas).then(response => {
     roleList.value = response.data.users;
     total.value = response.data.total;
     loading.value = false;
   });
 }
 
 /** 搜索按钮操作 */
 function handleQuery() {
   queryParams.value.pageNum = 1;
   getList();
 }
 
 /** 重置按钮操作 */
 function resetQuery() {
   dateRange.value = [];
   proxy.resetForm("queryRef");
   handleQuery();
 }
 
 /** 删除按钮操作 */
 function handleDelete(row) {
   const roleIds = row.roleId || ids.value;
   proxy.$modal.confirm('是否确认删除角色编号为"' + roleIds + '"的数据项?').then(function () {
     return delRole(roleIds);
   }).then(() => {
     getList();
     proxy.$modal.msgSuccess("删除成功");
   }).catch(() => {});
 }
 
 /** 导出按钮操作 */
 function handleExport() {
   proxy.download("system/role/export", {
     ...queryParams.value,
   }, `role_${new Date().getTime()}.xlsx`);
 }
 
 /** 多选框选中数据 */
 function handleSelectionChange(selection) {
   ids.value = selection.map(item => item.roleId);
   single.value = selection.length != 1;
   multiple.value = !selection.length;
 }
 
 /** 角色状态修改 */
 function handleStatusChange(row) {
   let text = row.status === "0" ? "启用" : "停用";
   proxy.$modal.confirm('确认要"' + text + '""' + row.roleName + '"角色吗?').then(function () {
     return changeRoleStatus(row.roleId, row.status);
   }).then(() => {
     proxy.$modal.msgSuccess(text + "成功");
   }).catch(function () {
     row.status = row.status === "0" ? "1" : "0";
   });
 }
 function gender(row,value){
    switch(value){
      case 0:
        return "保密"
      case 1:
        return '男'
      case 2:
        return '女'
    } 
 }
 
 /** 更多操作 */
 function handleCommand(command, row) {
   switch (command) {
     case "handleDataScope":
       handleDataScope(row);
       break;
     case "handleAuthUser":
       handleAuthUser(row);
       break;
     default:
       break;
   }
 }
 
 /** 分配用户 */
 function handleAuthUser(row) {
   router.push("/system/role-auth/user/" + row.roleId);
 }
 
 /** 查询菜单树结构 */
 function getMenuTreeselect() {
   menuTreeselect().then(response => {
     menuOptions.value = response.data;
   });
 }
 
 /** 所有部门节点数据 */
 function getDeptAllCheckedKeys() {
   // 目前被选中的部门节点
   let checkedKeys = deptRef.value.getCheckedKeys();
   // 半选中的部门节点
   let halfCheckedKeys = deptRef.value.getHalfCheckedKeys();
   checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
   return checkedKeys;
 }
 
 /** 重置新增的表单以及其他数据  */
 function reset() {
   if (menuRef.value != undefined) {
     menuRef.value.setCheckedKeys([]);
   }
   menuExpand.value = false;
   menuNodeAll.value = false;
   deptExpand.value = true;
   deptNodeAll.value = false;
   form.value = {
     roleId: undefined,
     roleName: undefined,
     roleKey: undefined,
     roleSort: 0,
     status: "0",
     menuIds: [],
     deptIds: [],
     menuCheckStrictly: true,
     deptCheckStrictly: true,
     remark: undefined
   };
   proxy.resetForm("roleRef");
 }
 
 /** 添加角色 */
 function handleAdd() {
   reset();
   getMenuTreeselect();
   open.value = true;
   title.value = "添加用户";
   type.value = "add";
 }
 
 /** 修改角色 */
 function handleUpdate(row) {
   reset();
   const roleId = row.userID;
   open.value= true
   title.value = "编辑用户";
   form.value = row
 }
 
 /** 取消按钮 */
 function cancel() {
   open.value = false;
   reset();
 }
 
 /** 取消按钮（数据权限）*/
 function cancelDataScope() {
   openDataScope.value = false;
   reset();
 }
 
 getList();
 </script>
<style scoped>
.upload_c{
    margin-bottom: 10px;
}
.upload_text{
    text-align: center;
    margin: 0 auto;
    margin-top: 10px;
    color: #1890ff;
    font-size: 14px;
}
.upload{
    width: 53px;
    height: 53px;
    border-radius: 10px;
    padding: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #f5f5f5;
    margin: 0 auto;
}
</style>
 